;; Entity to Point List  -  Lee Mac
;; Returns a list of WCS points describing or approximating the supplied entity, else nil if the entity is not supported.
;; ent - [ent] Entity name to be described by point list (POINT/LINE/ARC/CIRCLE/LWPOLYLINE/POLYLINE/ELLIPSE/SPLINE)
;; acc - [num] Positive number determining the point density for non-linear objects

(defun LM:ent->pts ( ent acc / ang bul cen cls di1 di2 enx inc itm lst num ocs rad tot typ vt1 vt2 vtl )
    (setq enx (entget ent)
          typ (cdr (assoc 0 enx))
    )
    (cond
        (   (= "POINT" typ)
            (list (cdr (assoc 10 enx)))
        )
        (   (= "LINE" typ)
            (mapcar '(lambda ( x ) (cdr (assoc x enx))) '(10 11))
        )
        (   (or (= "ARC" typ) (= "CIRCLE" typ))
            (if (= "ARC" typ)
                (setq ang (cdr (assoc 50 enx))
                      tot (rem (+ pi pi (- (cdr (assoc 51 enx)) ang)) (+ pi pi))
                      num (fix (+ 1.0 1e-8 (* acc (/ tot (+ pi pi)))))
                      inc (/ tot (float num))
                      num (1+ num)
                )
                (setq ang 0.0
                      tot (+ pi pi)
                      num (fix (+ 1e-8 acc))
                      inc (/ tot (float num))
                )
            )
            (setq cen (cdr (assoc 010 enx))
                  rad (cdr (assoc 040 enx))
                  ocs (cdr (assoc 210 enx))
            )
            (repeat num
                (setq lst (cons (trans (polar cen ang rad) ocs 0) lst)
                      ang (+ ang inc)
                )
            )
            (reverse lst)
        )
        (   (or (= "LWPOLYLINE" typ)
                (and (= "POLYLINE" typ) (zerop (logand (logior 16 64) (cdr (assoc 70 enx)))))
            )
            (if (= "LWPOLYLINE" typ)
                (setq vtl (LM:ent->pts:lwpolyvertices enx))
                (setq vtl (LM:ent->pts:polyvertices   ent))
            )
            (if (setq ocs (cdr (assoc 210 enx))
                      cls (= 1 (logand 1 (cdr (assoc 70 enx))))
                )
                (setq vtl (append vtl (list (cons (caar vtl) 0.0))))
            )
            (while (setq itm (car vtl))
                (setq vtl (cdr vtl)
                      vt1 (car itm)
                      bul (cdr itm)
                      lst (cons (trans vt1 ocs 0) lst)
                )
                (if (and (not (equal 0.0 bul 1e-8)) (setq vt2 (caar vtl)))
                    (progn
                        (setq rad (/ (* (distance vt1 vt2) (1+ (* bul bul))) 4.0 bul)
                              cen (polar vt1 (+ (angle vt1 vt2) (- (/ pi 2.0) (* 2.0 (atan bul)))) rad)
                              rad (abs rad)                            
                              tot (* 4.0 (atan bul))
                              num (fix (+ 1.0 1e-8 (* acc (/ (abs tot) (+ pi pi)))))
                              inc (/ tot (float num))
                              ang (+ (angle cen vt1) inc)
                        )                        
                        (repeat (1- num)
                            (setq lst (cons (trans (polar cen ang rad) ocs 0) lst)
                                  ang (+ ang inc)
                            )
                        )
                    )
                )
            )
            (reverse (if cls (cdr lst) lst))
        )
        (   (= "ELLIPSE" typ)
            (setq di1 (vlax-curve-getdistatparam ent (vlax-curve-getstartparam ent))
                  di2 (vlax-curve-getdistatparam ent (vlax-curve-getendparam   ent))
                  di2 (- di2 1e-8)
            )
            (while (< di1 di2)
                (setq lst (cons (vlax-curve-getpointatdist ent di1) lst)
                      rad (distance '(0.0 0.0) (vlax-curve-getfirstderiv ent (vlax-curve-getparamatdist ent di1)))
                      di1 (+ di1 (/ di2 (1+ (fix (* acc (/ di2 rad (+ pi pi)))))))
                )
            )
            (reverse (if (vlax-curve-isclosed ent) lst (cons (vlax-curve-getendpoint ent) lst)))
        )
        (   (= "SPLINE" typ)
            (setq di1 (vlax-curve-getdistatparam ent (vlax-curve-getstartparam ent))
                  di2 (vlax-curve-getdistatparam ent (vlax-curve-getendparam   ent))
                  lst (list (vlax-curve-getstartpoint ent))
                  inc (/ (- di2 di1) (float acc))
                  di1 (+ di1 inc)
            )
            (repeat (1- (fix (+ 1e-8 acc)))
                (setq lst (cons (vlax-curve-getpointatdist ent di1) lst)
                      di1 (+ di1 inc)
                )
            )
            (reverse (if (vlax-curve-isclosed ent) lst (cons (vlax-curve-getendpoint ent) lst)))
        )
    )
)

(defun LM:ent->pts:lwpolyvertices ( enx / elv lst vtx )
    (setq elv (list (cdr (assoc 38 enx))))
    (while (setq vtx (assoc 10 enx))
        (setq enx (cdr (member vtx enx))
              lst (cons (cons (append (cdr vtx) elv) (cdr (assoc 42 enx))) lst)
        )
    )
    (reverse lst)
)

(defun LM:ent->pts:polyvertices ( ent / lst vte vtx )
    (setq vte (entnext ent)
          vtx (entget  vte)
    )   
    (while (= "VERTEX" (cdr (assoc 0 vtx)))
        (setq lst (cons (cons (cdr (assoc 10 vtx)) (cdr (assoc 42 vtx))) lst)
              vte (entnext vte)
              vtx (entget  vte)
        )
    )
    (reverse lst)
)